package common.concurrency.baeldung.thread;

/**
 * @author zhang hao dong
 * @date 2025-06-04 17:31
 */
public class DaemonThread {
    public static void main(String[] args) throws InterruptedException {
        //Thread userThread = new Thread(() -> {
        //    for (int i = 0; i < 5; i++) {
        //        System.out.println("普通线程: " + i);
        //        try {
        //            Thread.sleep(1000);
        //        } catch (InterruptedException e) {
        //            e.printStackTrace();
        //        }
        //    }
        //});
        //userThread.start();
        //System.out.println("主线程结束");

        Thread daemonThread = new Thread(() -> {
            while (true) {
                System.out.println("守护线程运行中...");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        daemonThread.setDaemon(true); // 设置为守护线程
        daemonThread.start();
        Thread.sleep(3000); // 主线程等待一段时间后结束
        System.out.println("主线程结束");
    }
}
